package com.pd.modules.server.dialogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import com.pd.modules.common.Reservation;
import com.pd.modules.server.database.DBReservationsHandler;
import com.pd.modules.server.database.DataBase;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Observable;
import java.util.Observer;

@SuppressWarnings("serial")
public class DialogReservations extends JDialog implements Observer
{
	private JTable mTable;
	private String[] mTableColumns = new String[] { "ID", "Sala", "Data Inicio", "Data Fim", "Username" };
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JButton mButtonDelete;
	private DefaultTableModel mTableModel = null;

	/**
	 * Create the dialog.
	 */
	public DialogReservations()
	{
		setResizable(false);

		setModalityType(ModalityType.APPLICATION_MODAL);
		setModal(true);
		setTitle("Gerir Reservas");
		setBounds(100, 100, 565, 355);
		getContentPane().setLayout(new BorderLayout());

		DataBase.getInstance().Reservations().addObserver(this);

		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		{
			mButtonDelete = new JButton("Eliminar");
			mButtonDelete.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent arg0)
				{
					int idx = mTable.getSelectedRow();
					int reservationID = (int) mTable.getModel().getValueAt(idx, 0);

					DataBase.getInstance().Reservations().deleteRoomReservation(reservationID);

					mButtonDelete.setEnabled(false);
				}
			});
			mButtonDelete.setEnabled(false);
			buttonPane.add(mButtonDelete);

			JButton mButtonNew = new JButton("Nova...");
			mButtonNew.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					DialogRooms dialog = new DialogRooms("Escolha a sala...");
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				}
			});
			buttonPane.add(mButtonNew);
			getRootPane().setDefaultButton(mButtonNew);
		}

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));

		JPanel panelFilters = new JPanel();
		FlowLayout fl_panelFilters = (FlowLayout) panelFilters.getLayout();
		fl_panelFilters.setAlignment(FlowLayout.LEFT);
		panel.add(panelFilters, BorderLayout.NORTH);

		JRadioButton radioAll = new JRadioButton("Todas");
		buttonGroup.add(radioAll);
		radioAll.setSelected(true);
		radioAll.setHorizontalAlignment(SwingConstants.LEFT);
		panelFilters.add(radioAll);

		JScrollPane panelTable = new JScrollPane();
		panel.add(panelTable, BorderLayout.CENTER);
		{
			mTable = new JTable(null, mTableColumns)
			{
				@Override
				public boolean isCellEditable(int rowIndex, int colIndex)
				{
					return false; // Disallow the editing of any cell
				}
			};

			panelTable.setViewportView(mTable);

			mTable.getSelectionModel().addListSelectionListener(new EventSelectionChanged());
			mTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		}

		showAllReservations();
	}

	private void showAllReservations()
	{
		DBReservationsHandler db = DataBase.getInstance().Reservations();

		Reservation[] reservations = db.getAllReservations();

		Object[][] rows = new Object[reservations.length][5];
		for (int i = 0; i < reservations.length; i++)
		{
			rows[i] = reservations[i].getAsTableRow();
		}

		refreshTable(rows);
	}

	private void refreshTable(Object[][] _data)
	{
		mTableModel = new DefaultTableModel(_data, mTableColumns);
		mTable.setModel(mTableModel);

		// Alinha o texto ao centro
		DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
		dtcr.setHorizontalAlignment(SwingConstants.CENTER);
		for (String columnName : mTableColumns)
			mTable.getColumn(columnName).setCellRenderer(dtcr);
	}

	class EventSelectionChanged implements ListSelectionListener
	{

		@Override
		public void valueChanged(ListSelectionEvent e)
		{
			mButtonDelete.setEnabled(true);
		}
	}

	@Override
	public void update(Observable arg0, Object arg1)
	{
		showAllReservations();
	}

}
